/*
 * cross_serial_port.h
 *
 *  Created on: 2010-3-26
 *      Author: Administrator
 */

#ifndef CROSS_SERIAL_PORT_H_
#define CROSS_SERIAL_PORT_H_

#define SERIAL_PORT_WINDOWS 1
#define SERIAL_PORT_LINUX   2
#define SERIAL_PORT_TYPE SERIAL_PORT_WINDOWS

#if (SERIAL_PORT_TYPE == SERIAL_PORT_WINDOWS)
#include "windows_serial_port.h"
#else
#endif

/**************************************************************************
This header defines a cross-platform serial interface class
that was developed over the course of the ECE capstone class.
It is intended for demonstration purposes only and, while useful,
is not sufficient for most large applications. While we use these
functions in our project, they most likely do not apply
to all applications,and as always, Your Mileage May Vary(YMMV)
With that in mind, I hope that at the very least, you find this useful.
***************************************************************************/

class CrossPlatformSerial {
public:
	/**************************************************************
	 * Default constructor: create an instance of our serial interface
	 *   object.
	 * Parameters: None, though it could be convenient to have default
	 *   settings specified here as well as possibly a default port name.
	 * Returns: Nothing(it is a constructor)
	 */
	CrossPlatformSerial();
	/*
	 * Default deconstructor: delete an instance of out serial interface
	 */
	~CrossPlatformSerial();
	/*
	 * exist: Test serial port is existing
	 * Parameters:
	 *   portName: an array of chars containing a NULL terminated string
	 * Returns: a boolean indicating existing serial port
	 */
	static bool exist(char* portName);
	/*
	 * open: opens the serial port using port-name provided.
	 * Parameters:
	 *   portName: an array of chars containing a NULL terminated string
	 *     that is the port name that we want to open.
	 *     eg. COM1, COM2, COM4
	 * Returns: a boolean indicating success or failure
	 */
	bool open(char* portName);
	/*
	 * close: closes the serial port
	 * Parameters: None.
	 * Returns: a boolean indicating success or failure
	 */
	bool close();
	/*
	 * setSettings: Set the settings of the serial port interface. Should
	 *   be called after the port is opened because otherwise settings
	 *   could be lost.
	 * Parameters:
	 *   speed: the desired speed of the serial port
	 *   wordSize: the size of the data word in bits
	 *   stopBits: the number of stop bits
	 *   parity: parity to be used. One of
	 *     'n' = None
	 *     'e' = Even
	 *     'o' = Odd
	 * Returns: boolean indicating success or failure
	 */
	bool setSettings(int speed, int wordSize, int stopBits, char parity);
	/*
	 * setTimeouts: set the read and write timeouts of the serial port
	 * Parameters:
	 *   readTimeout: The amount of time to wait for a single word read to timeout (ms)
	 *   writeTimeout: The amount of time to wait for a single word write to timeout (ms)
	 * Returns: boolean indicating success or failure
	 */
	bool setTimeouts(int readTimeout, int writeTimeout);
	/*
	 * readBuf: read in a buffer of words
	 * Parameters:
	 *   buf: The buffer to read words into
	 *   bufSize: the number of words to read. This should be less than
	 *     or equal to the size of buf
	 * Returns: the number of words actually read
	 */
	int readBuf(char * buf, int bufSize);
	/*
	 * writeBuf: write out a buffer of words
	 * Parameters:
	 *   buf: The buffer to write words from
	 *   bufSize: the number of words to write. This should be less than
	 *     or equal to the size of buf
	 * Returns: the number of words actually written
	 */
	int writeBuf(char * buf, int bufSize);

private:
#if (SERIAL_PORT_TYPE == SERIAL_PORT_WINDOWS)
	WindowsSerialPort serialPort;
#else
#endif
};


#endif /* CROSS_SERIAL_PORT_H_ */
